/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.table.model;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import model.Activity;

/**
 *
 * @author Mads Nielsen <k4emic@gmail.com>
 */
public class ActivityTableModel extends AbstractTableModel {
    
    private ArrayList<Activity> activities;
    
    public static final int COL_START = 0;
    public static final int COL_END = 1;
    public static final int COL_NAME = 2;
    public static final int COL_ROOM = 3;

    private String[] colNames = {"Start", "Slut", "Navn", "Sted"};
    private Class[] colTypes = {String.class, String.class, String.class, String.class};

    public ActivityTableModel() {
        super();
        
        activities = new ArrayList<Activity>();
    }
    
    public void addActivity(Activity activity) {
        activities.add(activity);
    }

    @Override
    public int getRowCount() {
        return activities.size();
    }

    @Override
    public int getColumnCount() {
        return colNames.length;
    }

    @Override
    public String getColumnName(int index) {
        return colNames[index];
    }

    @Override
    public Class getColumnClass(int index) {
        return colTypes[index];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Activity activity = getActivityFromIndex(rowIndex);
        
        String timeFormat = "HH:mm";
        SimpleDateFormat sdf;

        switch (columnIndex) {
            case COL_START:
                sdf = new SimpleDateFormat(timeFormat);
                return sdf.format(activity.getTimeOfStart());
            case COL_END:
                sdf = new SimpleDateFormat(timeFormat);
                return sdf.format(activity.getTimeOfEnd());
            case COL_NAME:
                return activity.getName();
            case COL_ROOM:
                return activity.getRoom().getName();
            default:
                return null;
        }
    }

    public Activity getActivityFromIndex(int index) {
        if(index == -1){
            return null;
        }
        return activities.get(index);
    }
    
}
